home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mntdoc01.zoo / mintdoc / c_src / getpure.c next >
Encoding:
C/C++ Source or Header  |  1993-03-03  |  6.7 KB  |  226 lines

  1. /********************************************************************/
  2. /* getpure.c - read pages from an indexed Pure-C help source        */
  3. /*                                                                  */
  4. /* Copyright (c) 1993 by Hildo Biersma - Evil Eye Software          */
  5. /*                            e-mail: boender@dutiws.twi.tudelft.nl */
  6. /*        Evil Eye Software - ``Software with a Purpose''           */
  7. /*                                                                  */
  8. /* Freeware - do with this what you like, but leave my name.        */
  9. /********************************************************************/
  10. /*                                                                  */
  11. /* Usage: getpure man-page [....]                                   */
  12. /*        getpure prefix+                                           */
  13. /*                                                                  */
  14. /********************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "pureind.h"
  20.  
  21. /* Local function prototypes */
  22. void read_index(void);
  23. void display_pages(const char *page_name);
  24. void display_page(const struct page_record *page_ptr);
  25. int  compare_name(const void *, const void *);
  26. int  compare_prefix(const void *, const void *);
  27.  
  28. /* Global variables */
  29. struct file_record *files;
  30. struct page_record *pages;
  31. char                        *program_name;
  32. long                        no_files, no_pages;
  33. FILE                     *current_file = NULL;
  34. char                     current_file_no = ILLEGAL_FILE;
  35.  
  36. /* Read the index file into memory */
  37. void read_index(void)
  38. {
  39.   FILE *index;
  40.   char buf[4];
  41.  
  42.   if ((index = fopen(INDEXFILE_NAME, "rb")) == NULL)
  43.   {
  44.     fprintf(stderr, "%s: could not open index file\n", program_name);
  45.     exit(1);
  46.   }
  47.  
  48.   fread(buf, 4, sizeof(char), index);
  49.   if (strncmp(buf, INDEXFILE_HEADER, 4) != 0)
  50.   {
  51.     fprintf(stderr, "%s: index file is of wrong type\n", program_name);
  52.     exit(1);
  53.   }
  54.  
  55.   fread(&no_files, 1, sizeof(long), index);
  56.   fread(&no_pages, 1, sizeof(long), index);
  57.   files = malloc(no_files * sizeof(struct file_record));
  58.   pages = malloc(no_pages * sizeof(struct page_record));
  59.   if ((files == NULL) || (pages == NULL))
  60.   {
  61.     fprintf(stderr, "%s: memory allocation error\n", program_name);
  62.     exit(1);
  63.   }
  64.  
  65.   fread(files, no_files, sizeof(struct file_record), index);
  66.   fread(pages, no_pages, sizeof(struct page_record), index);
  67.   fclose(index);
  68. } /* End of read_index() */
  69.  
  70. /* Display pages from pure-c help source files */
  71. void display_pages(const char *page_name)
  72. {
  73.   char               search_string[PAGENAME_SIZE + 1];
  74.   struct page_record *page_ptr;
  75.   const char         *ptr;
  76.   size_t             pos;
  77.   int (*cmp_func)(const void *, const void *);
  78.  
  79.   if ((ptr = strchr(page_name, '+')) == NULL)
  80.   {
  81.     strncpy(search_string, page_name, PAGENAME_SIZE);
  82.     search_string[PAGENAME_SIZE] = 0x00;
  83.     strlwr(search_string);
  84.     cmp_func = compare_name;
  85.   }
  86.   else
  87.   {
  88.     pos = ptr - page_name;
  89.     strncpy(search_string, page_name, pos);
  90.     search_string[pos] = 0x00;
  91.     strlwr(search_string);
  92.     cmp_func = compare_prefix;
  93.   }
  94.  
  95.   if ((page_ptr = bsearch(&search_string, pages, no_pages,
  96.                                                   sizeof(struct page_record), cmp_func)) == NULL)
  97.   {
  98.     fprintf(stderr, "%s: page %s not found\n", program_name, 
  99.                     search_string);
  100.     return;
  101.   }
  102.  
  103.   while ((page_ptr > pages) &&
  104.                (cmp_func(page_ptr - 1, search_string) == 0))
  105.     page_ptr--;
  106.  
  107.   while ((page_ptr < pages + no_pages) &&
  108.          (cmp_func(page_ptr, search_string) == 0))
  109.     display_page(page_ptr++);
  110. } /* End of display_pages() */
  111.  
  112. /* Write one pure-c help source page to stdout */
  113. void display_page(const struct page_record *page_ptr)
  114. {
  115.     int  page_done = 0;
  116.     char line[BUFSIZ], buf[BUFSIZ];
  117.     
  118.   if (page_ptr->fileno != current_file_no)
  119.   {
  120.     if (current_file != NULL)
  121.       fclose(current_file);
  122.     current_file_no = page_ptr->fileno;
  123.     if ((current_file = fopen(files[current_file_no].name,
  124.                                                         "rt")) == NULL)
  125.     {
  126.       current_file_no = ILLEGAL_FILE;
  127.       fprintf(stderr, "%s: could not open source file %s\n",
  128.               program_name, files[current_file_no].name);
  129.       return;
  130.     }
  131.   }
  132.  
  133.   fseek(current_file, page_ptr->pos, SEEK_SET);
  134.   while ((page_done == 0) && !feof(current_file))
  135.   {
  136.       char *ptr1 = line, *ptr2 = buf;
  137.       
  138.       fgets(line, BUFSIZ, current_file);
  139.       while (*ptr1 != 0x00)
  140.       {
  141.         if (*ptr1 != '\\')
  142.           *ptr2++ = *ptr1++;
  143.         else
  144.         {
  145.           if (ptr1[1] == '\\')  /* \\ means \ */
  146.           {
  147.             *ptr2++ = '\\';
  148.             ptr1 += 2;
  149.           }
  150.           else if (ptr1[1] == '#') /* Skip over \#...\# */
  151.           {
  152.             ptr1 += 2;
  153.             while ((*ptr1 != '\\') && (ptr1[1] != '#'))
  154.               *ptr2++ = *ptr1++;
  155.             ptr1 += 2;
  156.           }
  157.           else if ((ptr1[1] == 'l') && /* Skip over \link("...")...\# */
  158.                    (ptr1[2] == 'i') &&
  159.                    (ptr1[3] == 'n') &&
  160.                    (ptr1[4] == 'k'))
  161.           {
  162.             ptr1 += 7; /* Skip over \link(" */
  163.             while ((*ptr1 != '"') && (ptr1[1] != ')'))
  164.               ptr1++;
  165.             ptr1 += 2;
  166.             while ((*ptr1 != '\\') && (ptr1[1] != '#'))
  167.               *ptr2++ = *ptr1++;
  168.             ptr1 += 2;
  169.           }
  170.           else if ((ptr1[1] == 'e') && /* End page on \end */
  171.                    (ptr1[2] == 'n') &&
  172.                    (ptr1[3] == 'd'))
  173.           {
  174.             *ptr2++ = '\n';
  175.             *ptr2++ = '\n';
  176.             *ptr1 = 0x00;
  177.             page_done = 1;
  178.           }
  179.           else
  180.           {
  181.             fprintf(stderr, "%s: choked on line: %s\n",
  182.                     program_name, line);
  183.             exit(1);
  184.           }
  185.         }
  186.       } /* End of while (line not copied) */
  187.       *ptr2 = 0x00;
  188.       fputs(buf, stdout);
  189.     } /* End of while (not end of page or file) */
  190. } /* End of display_page() */
  191.  
  192. /* Compare page name and search string (full length) */
  193. int compare_name(const void *page, const void *name)
  194. {    
  195.     struct page_record *page_ptr ;
  196.     const char                 *name_ptr;
  197.     
  198.     page_ptr = (struct page_record *)page;
  199.     name_ptr = (char *)name;
  200.  
  201.   return(strncmp(page_ptr->name, name_ptr, PAGENAME_SIZE));
  202. } /* End of compare_name() */
  203.  
  204. /* Compare page name and search string (prefix) */
  205. int compare_prefix(const void *page, const void *prefix)
  206. {
  207.     struct page_record *page_ptr ;
  208.     const char                 *prefix_ptr;
  209.     
  210.     page_ptr = (struct page_record *)page;
  211.     prefix_ptr = (char *)prefix;
  212.     
  213.   return(strncmp(page_ptr->name, prefix_ptr, strlen(prefix_ptr)));
  214. } /* End of compare_prefix() */
  215.  
  216. void main(int argc, char *argv[])
  217. {
  218.   int counter = 1;
  219.  
  220.   program_name = argv[0];
  221.   read_index();
  222.   while (counter < argc)
  223.     display_pages(argv[counter++]);
  224.   exit(0);
  225. } /* End of main() */
  226.